package pt.ipl.isel.deetc.leic.pdm.g12.yamba;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class TimelinePullService extends Service implements Runnable {	
	private volatile boolean terminate = false;
	private Thread worker;
	private YambaPdmApp yamba;	
	
	public void onCreate() {
		super.onCreate();
		this.yamba = (YambaPdmApp) getApplication(); 
		worker = new Thread(this, "Worker"); 
		worker.start();
	}
	
	public int onStartCommand(Intent intent, int flgs, int id) {
		return START_STICKY;
	}
	
	public void onDestroy() {
		terminate = true; 
		worker.interrupt();
		super.onDestroy();
	}
	
	public IBinder onBind(Intent intent) { return null; }

	public void run() {
		while(!terminate)
			try {
				yamba.updateStatusList();
				String timelineUpdateInterval = yamba.getPreferences().getString("timelineUpdateInterval", "5000");
				Thread.sleep(Integer.parseInt(timelineUpdateInterval) * 1000);				
			}
		catch (InterruptedException e) { }
	}	
}
